package no.rmz.chez.game;

import no.rmz.chez.chess.board.Coord;
import no.rmz.chez.chess.pieces.AlgebraicChessNotationElement;
import no.rmz.chez.chess.pieces.ChessPiece;

/**
 * Describe a move on a chessboard.
 */
public class Move implements AlgebraicChessNotationElement {
    /**
     * If the move involves converting the moving piece into something
     * else, this is the piece we�re being converted into.
     */
    private ChessPiece transformingPiece;

    /**
     * The location where the move starts.
     */
    private Coord source;

    /**
     * The location where the move ends.
     */
    private Coord dest;

    /**
     * The chesspiece being moved. (some issues here regarding an passant and
     * casteling maneuvers, but since we're not handling them yet we'll just let
     * them be for now).
     */
    private final ChessPiece piece;


     /**
     * Make a new move.
     * @param s source coordinate.
     * @param d destination coordinate.
     */
    public Move(final ChessPiece piece,final Coord s, final Coord d) {
        super();
        this.piece  = piece;
        this.source = s;
        this.dest   = d;
    }

    public ChessPiece getPiece() {
        return piece;
    }



    /**
     * @return a text representation of a move.
     */
   
    public final String pedanticToString() {
        final StringBuilder sb = new StringBuilder();
        sb.append(String.format("[%s->%s",getSource(),  getDest()));
        if (isTransformingMove()) {
            sb.append(", transforming to ").append(transformingPiece);
        }
        if (isCapturing) {
            sb.append(", capturing something");
        }
        sb.append("]");
        return sb.toString();
    }

    @Override
    public String toString() {
        return getACNRepresentation();
    }

    @Override
    public String getACNRepresentation() {
        final StringBuilder sb = new StringBuilder();
        sb.append(getPiece().getACNRepresentation());
        sb.append(getSource().getACNRepresentation());
        sb.append(isCapturing()?"x":" ");
        sb.append(getDest().getACNRepresentation());
        if (transformingPiece != null) {
            sb.append("=").append(transformingPiece.getACNRepresentation());
        }
       
        return sb.toString();
    }


   
    /**
     * Annotation describing if the move captures something.
     */
    private boolean isCapturing = false;

    /**
     * Is the move capturing something?
     * @return is capturing?
     */
    public  final boolean isCapturing() {
        return isCapturing;
    }

    /**
     * Add annotation to describe if the move is capturing or not.
     * @param capt true iff the move is capturing.
     */
    public final void setCapturing(final boolean capt) {
        this.isCapturing = capt;
    }

    /**
     * Get the destination.
     * @return destination.
     */
    public final Coord getDest() {
        return dest;
    }

    /**
     * Get the source.
     * @return the source.
     */
    public final Coord getSource() {
        return source;
    }

    /**
     * If a transformation is happening (e.g. transforming a
     * pawn into a queen), get the piece we're transforming to.
     * @return the transforming piece.
     */
    public final ChessPiece getTransformingPiece() {
        return transformingPiece;
    }


    /**
     * True iff this is a transforming move.
     * @return true iff we're transforming.
     */
    public final boolean isTransformingMove() {
        return transformingPiece != null;
    }

    /**
     * Set which piece to transform to.
     * @param p the piece we're transforming to.
     */
    public final void transformMovingPieceTo(final ChessPiece p) {
        this.transformingPiece = p;
    }
}
